package com.iwhalecloud.bss.kite.cucc.service.scene.query;

import com.google.common.base.Objects;
import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneMktResInstService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.dto.inst.MktResInstRelDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotBlank;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteMktResInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.scene.AbstractInstService;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("querySceneMktResInstService")
public class QuerySceneMktResInstService extends AbstractInstService implements IQuerySceneMktResInstService {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    /**
     * 获取可选包/促销包约束的终端实例
     *
     * @param custId 客户id
     * @param offerInstId 可选包/促销包销售品实例id
     * @return 终端实例列表
     */
    @Override
    @Validated
    public DataResult<List<MktResInstRelDTO>> listMktResInstsByOfferInstId(@KiteNotBlank String custId,
        @KiteNotBlank String offerInstId) {
        List<KiteMktResInst> kiteMktResInstList = querySceneInstService.listMktResInstsByOfferInstId(custId,
            offerInstId);
        List<MktResInstRelDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteMktResInstList)) {
            dtoList = CopyToDtoUtil.transMktResInstRelDtoList(kiteMktResInstList);
            // TODO 待确认 装箱
        }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    /***
     * 查询接入产品已经订购的终端列表
     *
     * @param sceneInstId
     * @param accessProdInstId
     * @param custId 客户id，分片字段，必填
     * @return
     */
    @Validated
    @Override
    public DataResult<List<MktResInstRelDTO>> listMktResInstByProdInstId(@KiteNotBlank String sceneInstId,
        @KiteNotBlank String accessProdInstId, @KiteNotBlank String custId) {
        List<KiteMktResInst> kiteMktResInstList = querySceneInstService.listMktResInstsByProdInstId(custId,
            accessProdInstId);
        List<MktResInstRelDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteMktResInstList)) {
            dtoList = CopyToDtoUtil.transMktResInstRelDtoList(kiteMktResInstList);
            dtoList = dtoList.stream().filter((res) -> !Objects.equal(ActionType.D.name(), res.getActionType())).collect(Collectors.toList());
            // TODO 待确认 装箱
        }
        // else {
        // dtoList = queryMktResInstRelBO.listMktResInstRelDtoByAccessProdInstId(accessProdInstId, custId);
        // checkAndPutMktResInstRelList(dtoList, queryMktResInstRelBO);
        // }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    /***
     * 查询终端实例属性
     *
     * @param sceneInstId
     * @param offerResInstRelId 终端产品实例标识
     * @param custId 客户id，分片字段，必填
     * @return
     */
    @Validated
    @Override
    public DataResult<List<InstAttrDTO>> listMktResInstAttrByProdInstId(@KiteNotBlank String sceneInstId,
        @KiteNotBlank String offerResInstRelId, @KiteNotBlank String custId) {
        List<KiteAttr> kiteAttrList = querySceneInstService.listMktResInstAttrs(custId, offerResInstRelId);
        List<InstAttrDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteAttrList)) {
            dtoList = CopyToDtoUtil.transInstAttrDtoList(kiteAttrList);
        }
        // else {
        // dtoList = queryMktResInstRelBO.listMktResInstRelAttrs(offerResInstRelId, custId);
        // checkAndPutAttr(dtoList, sceneInstId, offerResInstRelId);
        // }
        return ErrorConsts.SUCCESS.getResult(dtoList);
    }

    public DataResult<List<InstAttrDTO>> getMktResInstAttrs(String sceneInstId, String custId, String offerResInstRelId,
        String showType) {
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        KiteMktResInst inst = querySceneInstService.getMktResInst(custId, offerResInstRelId);
        List<KiteAttr> kiteAttrList = inst.listAttrs();

        kiteAttrList = kiteAttrList.stream()
            .filter(kiteAttr -> KiteStringUtils.equals(showType, kiteAttr.getShowType()) || KiteStringUtils.isEmpty(showType))
            .collect(Collectors.toList());

        operateInstAttrService.getKiteInstAttrs(inst, kiteAttrList, sceneInst);
        kiteAttrList = operateInstAttrService.sortProdAttr(kiteAttrList);

        List<InstAttrDTO> dtoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(kiteAttrList)) {
            dtoList = CopyToDtoUtil.transInstAttrDtoList(kiteAttrList);
        }

        return ErrorConsts.SUCCESS.getResult(dtoList);
    }


    /**
     * 查询可选包约束终端规格属性
     */
    @Override
    public DataResult<List<InstAttrDTO>> listAdditionalMktResAttrs(String sceneInstId, String additionalOfferId,
        String roleId, String prodId) {
//        Offer offer = OfferCache.get(additionalOfferId);
//        offer.listMustResProds(roleId, prodId)
        return null;
    }

    /**
     * 查询已订购终端列表
     */
    @Override
    public DataResult<List<MktResInstRelDTO>> listMktResInst(String sceneInstId, String prodInstId, String custId) {
        List<MktResInstRelDTO> result = listMktResInstByProdInstId(sceneInstId, prodInstId, custId).getResultObject();
        result.stream().forEach(mktResInstRelDTO -> {
            List<InstAttrDTO> instAttrList = getMktResInstAttrs(sceneInstId, custId,
                mktResInstRelDTO.getOfferResInstRelId(), KeyConsts.PROD_ATTR_SHOW_TYPE_D).getResultObject();
            if (KiteListUtils.isEmpty(instAttrList)) {
                mktResInstRelDTO.setRstrAttr(false);
            }
            else {
                mktResInstRelDTO.setRstrAttr(true);
            }
        });
        return ErrorConsts.SUCCESS.getResult(result);
    }
}
